package com.pl.rabarbar.database;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.pl.rabarbar.model.Book;

public class Cache extends LinkedHashMap {

	private LinkedHashMap<Integer, Book> cache = null;
	private final static int CACHE_MAX_SIZE = 3;

	public LinkedHashMap<Integer, Book> getCache() {
		return this.cache;
	}

	public Book getId(int id) {
		return this.cache.get(id);
	}

	public Book getName(String name) {
		Collection<Book> c = this.cache.values();
		Iterator itr = c.iterator();
		for (Map.Entry<Integer, Book> entry : this.cache.entrySet()) {

			Book value = entry.getValue();
			if (value.getName().contains(name))
				return value;
			// now work with key and value...
		}
		return null;
	}

	public void setCache(LinkedHashMap<Integer, Book> cache) {
		this.cache = cache;
	}

	public void putCache(Integer id, Book book) {
		this.cache.put(id, book);
	}

	public Cache() {

		this.cache = new LinkedHashMap<Integer, Book>(CACHE_MAX_SIZE, 75f, true) {
			@Override
			protected boolean removeEldestEntry(Entry eldest) {
				// TODO Auto-generated method stub
				return size() > CACHE_MAX_SIZE;
			}
		};
	}

	/*
	 * public CacheDAO(InterfaceDAO interfaceDAO) { this.interfaceDAO =
	 * interfaceDAO; this.cache = new LinkedHashMap<Integer,
	 * Book>(CACHE_MAX_SIZE, 75f, true){
	 * 
	 * @Override protected boolean removeEldestEntry(Entry eldest) { // TODO
	 * Auto-generated method stub return size() > CACHE_MAX_SIZE; } }; }
	 */

}
